
from models import execute


async def get_user_default_address(uid):
    sql = "select id, `name`, phone, area, detail from `address` where uid = %s and is_default = 1"
    _, address = await execute(sql, (uid,))
    return address


async def get_sku_info_from_sku_id(sku_id):
    if isinstance(sku_id, list):
        _, sku = await execute('select id, good_id, title, price, default_image from good_sku where id in (%s)' % ','.join(
            ['%s'] * len(sku_id)) % tuple(sku_id))
    else:
        _, sku = await execute('select id, good_id, title, price, default_image from good_sku where id = %s', (sku_id, ))
    return sku


async def get_property_from_sku_id(sku):
    _, property_values = await execute('select title from property_value where sku_id = %s', (sku.get('id')))
    return property_values


async def query_spu_is_exist(spu_id):
    _, spu = await execute('select id from good where id = %s', (spu_id,))
    return spu


async def get_sku_from_spu(spu_id):
    _, sku_ids = await execute('select id from good_sku where good_id = %s', (spu_id,))
    return sku_ids


async def query_spu_property_count(sku_id_count, new_sku_ids):
    _, res = await execute(
        'select count(distinct property_id) as total from property_value where sku_id in (%s);' % ','.join(
            ['%s'] * sku_id_count), tuple(new_sku_ids))
    return res


async def query_sku_from_property_title_skus(sku_id_count, new_sku_ids):
    _, res = await execute('select sku_id from property_value where sku_id in (%s)' % ','.join(
        ['%s'] * sku_id_count) + 'and title = %s', tuple(new_sku_ids))
    return res

